/*
 * create:2016年1月7日 下午10:17:07
 * author:wangds  
 */

package net.wangds.common.chainofresponsibility.test;

import junit.framework.TestCase;
import net.wangds.common.chainofresponsibility.AbstractHandler;
import net.wangds.common.chainofresponsibility.Handler;

import org.junit.Test;

/**
 * .
 * 
 * @author wangds
 *
 */
public class CorTestCase {

  static class H extends AbstractHandler {

    public static String STEP = null;

    private boolean cont;
    private String step = null;

    public H(boolean cont, String step) {
      this.cont = cont;
      this.step = step;
    }

    /*
     * @see net.wangds.common.chainofresponsibility.Handler#handle()
     * 
     * @author wangds 2016年1月7日 下午10:22:30.
     */
    @Override
    public boolean handle() {
      if (this.cont) {
        STEP = this.step;
      }
      return cont;
    }
  }

  @Test
  public void test() throws Exception {
    Handler h1 = new H(true, "1");
    Handler h2 = new H(true, "2");
    Handler h3 = new H(false, "3");

    h1.setNext(h2);
    h2.setNext(h3);
    TestCase.assertEquals(h1.getNext(), h2);
    h1.process();
    TestCase.assertEquals(H.STEP, "2");

    h1.setNext(h3);
    h3.setNext(h2);
    h2.setNext(null);
    TestCase.assertNull(h2.getNext());
    h1.process();
    TestCase.assertEquals(H.STEP, "1");

    h1.setNext(h2);
    h1.process();
    TestCase.assertEquals(H.STEP, "2");

  }

}
